package com.zhang.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zhang.gulimall.product.entity.ProductAttrValueEntity;
import com.zhang.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhang.gulimall.product.entity.AttrEntity;
import com.zhang.gulimall.product.service.AttrService;
import com.zhang.gulimall.common.utils.PageUtils;
import com.zhang.gulimall.common.utils.R;



/**
 * 商品属性
 *
 * @author zhang
 * @email sunlightcs@gmail.com
 * @date 2022-09-14 10:23:17
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    ///product/attr/base/listBySpu/{spuId}
    /**
     * 保存
     */
    @RequestMapping("/update/{spuId}")
    public R updateById(@RequestBody List<ProductAttrValueEntity> list,@PathVariable String spuId){
        attrService.updateAttr(list,spuId);

        return R.ok();
    }
    /**
     * 根据spuId获取属性
     */
    @RequestMapping("/base/listBySpu/{spuId}")
    public R listBySpu(@RequestParam Map<String, Object> params, @PathVariable String spuId){
        List<ProductAttrValueEntity> list = attrService.listBySpu(params, spuId);

        return R.ok().put("data", list);
    }
    /**
     * 根据skuId获取属性
     */
    @RequestMapping("/base/listBySpu/{skuId}")
    public R listBySpu(@PathVariable String skuId){
        List<ProductAttrValueEntity> list = attrService.listBySku(skuId);

        return R.ok().put("data", list);
    }
    /**
     * 列表
     */
    @RequestMapping("/base/list/{id}")
    public R baseList(@RequestParam Map<String, Object> params, @PathVariable String id){
        PageUtils page = attrService.queryBasePage(params, id);

        return R.ok().put("page", page);
    }

    /**
     * 销售列表
     */
    @RequestMapping("/sale/list/{id}")
    public R saleList(@RequestParam Map<String, Object> params, @PathVariable String id){
        PageUtils page = attrService.querySalePage(params, id);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") String attrId){
		AttrVo attr = attrService.getDetailById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateDetail(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
